﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using HuynhQuangWebsite.Models;
namespace HuynhQuangWebsite.Controllers
{
    [HandleError]
    public class ManageAdminController : BaseController
    {
        //
        // GET: /AdminManagerment/
        huynhquangco_websiteEntities ctx = new huynhquangco_websiteEntities();
        public int PageSize = 6;
        #region--Admin--
        public ActionResult IndexAdmin(int page = 1)
        {
          //  if (!IsAuthenticateAdmin())
            //    return RedirectToAction("LogOnDashBoard", "DashBoard");
            var model =new HomeAdminListViewModel();
            IEnumerable<Models.Admin> admins = ctx.Admins.ToList<Admin>();
            model.AdminList = admins.Skip((page-1) * PageSize).Take(PageSize).ToList();
            model.AdminInfo = new THS.HtmlHelpers.PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = admins.Count() };
            return View(model);
          
        }

        //
        // GET: /AdminManagerment/Details/5

        public ActionResult Detail(int id)
        {
            try
           {
               // if (!IsAuthenticateAdmin())
                 //  return RedirectToAction("LogOnDashBoard", "DashBoard");
                Models.Admin adm = ctx.Admins.Single(p => p.AdminID == id);
                if (adm != null)
                    return View(adm);
                return View();
            }
            catch
            {
                return View();
            }
        }
        //
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Detail(int id, FormCollection collection)
        {
            try
            {
                //if (!IsAuthenticateAdmin())
                //  return RedirectToAction("LogOn", "AdminHome");
                Models.Admin adm = ctx.Admins.Where(ad => ad.AdminID == id).SingleOrDefault();
                if (adm != null)
                {
                    EnCrypto crt = new EnCrypto();
                    adm.Password= crt.Encrypt(collection["Password"]);
                    ctx.SaveChanges();
                    ViewData["Msg"] = "Cập nhật thành công";
                }
                return RedirectToAction("Detail");
            }
            catch
            {
                ViewData["Msg"] = "Cập nhật không thành công.";
                return RedirectToAction("Detail");
            }
        }
        //
        // GET: /AdminManagerment/Create

        public ActionResult Create()
        {
           // if (!IsAuthenticateAdmin())
             //   return RedirectToAction("LogOnDashBoard", "DashBoard");
            Admin admin = new Admin();
            return View(admin);
        } 

        //
        // POST: /AdminManagerment/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create([Bind(Exclude = "AdminID")] Admin adm)
        {
           // Attempt to register the user


                    //if (!IsAuthenticateAdmin())
                    //   return RedirectToAction("LogOnDashBoard", "DashBoard");
                try
                {
                    //if (ModelState.IsValid)
                    //{


                        IEnumerable<Models.Admin> existAdmin = ctx.Admins.Where(p => p.UserName == adm.UserName).ToList();
                        if (existAdmin.Count() == 0)
                        {
                            EnCrypto enc = new EnCrypto();
                            adm.Password = enc.Encrypt(adm.Password);
                            adm.AllowLogOn = true;
                            ctx.AddToAdmins(adm);
                            ctx.SaveChanges();
                            return RedirectToAction("IndexAdmin");
                        }
                        else
                        {
                            ViewData["Msg"] = "Đã tồn tại tên đăng nhập";
                            return View();

                        }
                    //}
                    //else
                      //  return View();
                }
                catch
                {
                    return View("Create");
                }
            
        }
        
      

        //
        // POST: /AdminManagerment/Edit/5

     
        //allow Log On
        public void AllowLogOn(int id)
        {
            try
            {
                //if (!IsAuthenticateAdmin())
                  //  return;
                Admin adm = ctx.Admins.SingleOrDefault(p => p.AdminID == id);
                if (adm.AllowLogOn)
                    adm.AllowLogOn = false;
                else
                    adm.AllowLogOn = true;
                ctx.SaveChanges();


            }
            catch
            {
                return;
            }
        }
        //active,deactivate logon
        public ActionResult ActiveConsultants()
        {
            try
            {
                if (!IsAuthenticateAdmin())
                    return RedirectToAction("LogOn", "AdminHome");
                IEnumerable<Models.Admin> adms = ctx.Admins.Where(p => p.Type != "Admin").ToList();
                foreach (var item in adms)
                {
                    item.AllowLogOn = true;
                    ctx.SaveChanges();
                }
                return RedirectToAction("Index");

            }
            catch
            {
                return RedirectToAction("Index");
            }
        }
        //deactive
        public ActionResult DeactivateConsultants()
        {
            try
            {
                if (!IsAuthenticateAdmin())
                    return RedirectToAction("LogOn", "AdminHome");
                IEnumerable<Models.Admin> adms = ctx.Admins.Where(p => p.Type != "Admin").ToList();
                foreach (var item in adms)
                {
                    item.AllowLogOn = false;
                    ctx.SaveChanges();
                }
                return RedirectToAction("Index");

            }
            catch
            {
                return RedirectToAction("Index");
            }
        }
       //
        // GET: /AdminManagerment/Delete/5

        public ActionResult Delete(int id)
        {
            try
            {
                if (!IsAuthenticateAdmin())
                    return RedirectToAction("LogOn", "AdminHome");
                Admin adm = ctx.Admins.Single(p => p.AdminID == id);
                ctx.Admins.DeleteObject(adm);
                ctx.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public bool CheckPassword(int id, String pwd)
        {
            try
            {
                Models.Admin user = ctx.Admins.Where(p => p.AdminID == id).SingleOrDefault() as Admin;
                if (user == null)
                {
                    return false;
                }
                else
                {
                    EnCrypto crt = new EnCrypto();
                    if (crt.Encrypt(pwd) == user.Password)
                        return true;
                    else
                        return false;
                }
            }
            catch
            {
                return false;
            }
        }

        //check esxist user name
        public bool CheckName(string userName)
        {
            try
            {
                Models.Admin user = ctx.Admins.Where(p => p.UserName.Trim() == userName.Trim()).SingleOrDefault() as Admin;
                if (user != null)
                    return false;
                else
                    return true;
            }
            catch
            {
                return false;
            }
        }
      
        #endregion
       

    }
}
